﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace CPPEI.Service.SystemManagement
{
    public class SystemManagementService
    {
        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public ServiceModel.ServiceResult CreateUser(User user)
        {
            string sql = "insert into [SYS_User] (Id,Account,Name,Password,Email,Phone,CreatedTime) values (@Id,@Account,@Name,@Password,@Email,@Phone,@CreatedTime)";
            SqlParameter PId = new SqlParameter("@Id", user.Id);
            SqlParameter pAccount = new SqlParameter("@Account", user.Account);
            SqlParameter pName = new SqlParameter("@Name", user.Name);
            SqlParameter pPassword = new SqlParameter("@Password", user.Password);
            SqlParameter pEmail = new SqlParameter("@Email", user.Email);
            SqlParameter pPhone = new SqlParameter("@Phone", user.Phone);
            SqlParameter pCreatedTime = new SqlParameter("@CreatedTime", user.CreatedTime);
            SqlParameter[] sqlParameterArr = new SqlParameter[] { PId, pAccount, pName, pPassword, pEmail, pPhone, pCreatedTime };
            DataAccess.DB.ExecuteNonQuery(sql, sqlParameterArr);
            return new ServiceModel.ServiceResult();
        }

        /// <summary>
        /// 创建导航
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="navigation"></param>
        /// <returns></returns>
        public ServiceModel.ServiceResult CreateNavigation(Guid parentId, Navigation navigation)
        {
            string sql = "insert into [SYS_Navigation] (Id,Caption,SortIndex,ParentId,TargetType,TargetId,CreatedTime,Creator) values (@Id,@Caption,@SortIndex,@ParentId,@TargetType,@TargetId,@CreatedTime,@Creator)";
            SqlParameter PId = new SqlParameter("@Id", navigation.Id);
            SqlParameter pCaption = new SqlParameter("@Caption", navigation.Caption);
            SqlParameter pSortIndex = new SqlParameter("@SortIndex", navigation.SortIndex);
            SqlParameter pParentId = new SqlParameter("@ParentId", parentId);
            SqlParameter pTargetType = new SqlParameter("@TargetType", navigation.TargetType);
            SqlParameter pTargetId = new SqlParameter("@TargetId", navigation.TargetId);
            SqlParameter pCreatedTime = new SqlParameter("@CreatedTime", navigation.CreatedTime);
            SqlParameter pCreator = new SqlParameter("@Creator", navigation.Creator);
            SqlParameter[] sqlParameterArr = new SqlParameter[] { PId, pCaption, pSortIndex, pParentId, pTargetType, pTargetId, pCreatedTime, pCreator };
            DataAccess.DB.ExecuteNonQuery(sql, sqlParameterArr);

            Privilege privilege = new Privilege();
            privilege.Id = Guid.NewGuid();
            privilege.Caption = navigation.Caption;
            privilege.SortIndex = 0;
            privilege.Code = navigation.Id.ToString();
            privilege.TargetType = 0xFF01;
            privilege.TargetId = navigation.Id.ToString();
            privilege.CreatedTime = DateTime.Now;
            privilege.Creator = navigation.Creator;
            CreatePrivilege(privilege);
            
            return new ServiceModel.ServiceResult();
        }

        public ServiceModel.ServiceResult CreatePrivilege(Privilege privilege)
        {
            string sql = "insert into [SYS_Privilege] (Id,Caption,SortIndex,Code,TargetType,TargetId,CreatedTime,Creator) values (@Id,@Caption,@SortIndex,@Code,@TargetType,@TargetId,@CreatedTime,@Creator)";
            SqlParameter PId = new SqlParameter("@Id", privilege.Id);
            SqlParameter pCaption = new SqlParameter("@Caption", privilege.Caption);
            SqlParameter pSortIndex = new SqlParameter("@SortIndex", privilege.SortIndex);
            SqlParameter pCode = new SqlParameter("@Code", privilege.Code);
            SqlParameter pTargetType = new SqlParameter("@TargetType", privilege.TargetType);
            SqlParameter pTargetId = new SqlParameter("@TargetId", privilege.TargetId);
            SqlParameter pCreatedTime = new SqlParameter("@CreatedTime", privilege.CreatedTime);
            SqlParameter pCreator = new SqlParameter("@Creator", privilege.Creator);
            SqlParameter[] sqlParameterArr = new SqlParameter[] { PId, pCaption, pSortIndex, pCode, pTargetType, pTargetId, pCreatedTime, pCreator };
            DataAccess.DB.ExecuteNonQuery(sql, sqlParameterArr);
            return new ServiceModel.ServiceResult();
        }

        public List<User> GetUserList()
        {
            List<User> userList = new List<User>();

            string sql = "select [Id],[Account],[Name],[Email],[Phone],[CreatedTime] from [SYS_User]";
            DataTable dataTable = DataAccess.DB.QueryDataTable(sql);
            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                User user = new User();
                user.Id = new Guid(Convert.ToString(dataTable.Rows[index]["Id"]));
                user.Account = Convert.ToString(dataTable.Rows[index]["Account"]);
                user.Name = Convert.ToString(dataTable.Rows[index]["Name"]);
                user.Email = Convert.ToString(dataTable.Rows[index]["Email"]);
                user.Phone = Convert.ToString(dataTable.Rows[index]["Phone"]);
                user.CreatedTime = Convert.ToDateTime(dataTable.Rows[index]["CreatedTime"]);
                userList.Add(user);
            }

            return userList;
        }

        public List<Navigation> GetNavigationList(string account)
        {
            List<Navigation> navigationList = new List<Navigation>();

            string sql = "select [Id],[Caption],[TargetType],[TargetId],[CreatedTime],[Creator] from [SYS_Navigation]";
            DataTable dataTable = DataAccess.DB.QueryDataTable(sql);
            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                Navigation navigation = new Navigation();
                navigation.Id = new Guid(Convert.ToString(dataTable.Rows[index]["Id"]));
                navigation.Caption = Convert.ToString(dataTable.Rows[index]["Caption"]);
                navigation.TargetType = Convert.ToInt32(dataTable.Rows[index]["TargetType"]);
                navigation.TargetId = Convert.ToString(dataTable.Rows[index]["TargetId"]);
                navigation.CreatedTime = Convert.ToDateTime(dataTable.Rows[index]["CreatedTime"]);
                navigation.Creator = Convert.ToString(dataTable.Rows[index]["Creator"]);
                navigationList.Add(navigation);
            }

            return navigationList;
        }

        public Navigation GetNavigation(string account, int targetType, string targetId)
        {
            Navigation navigation = null;

            string sql = "select [Id],[Caption],[TargetType],[TargetId],[CreatedTime],[Creator] from [SYS_Navigation] where TargetType=@TargetType and TargetId=@TargetId";
            SqlParameter pTargetType = new SqlParameter("@TargetType", targetType);
            SqlParameter pTargetId = new SqlParameter("@TargetId", targetId);
            DataTable dataTable = DataAccess.DB.QueryDataTable(sql, new SqlParameter[] { pTargetType, pTargetId });
            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                navigation = new Navigation();
                navigation.Id = new Guid(Convert.ToString(dataTable.Rows[index]["Id"]));
                navigation.Caption = Convert.ToString(dataTable.Rows[index]["Caption"]);
                navigation.TargetType = Convert.ToInt32(dataTable.Rows[index]["TargetType"]);
                navigation.TargetId = Convert.ToString(dataTable.Rows[index]["TargetId"]);
                navigation.CreatedTime = Convert.ToDateTime(dataTable.Rows[index]["CreatedTime"]);
                navigation.Creator = Convert.ToString(dataTable.Rows[index]["Creator"]);
                break;
            }

            return navigation;
        }

        public List<Navigation> GetNavigationList(string account, Guid parentId)
        {
            List<Navigation> navigationList = new List<Navigation>();

            string sql = "select [Id],[Caption],[TargetType],[TargetId],[CreatedTime],[Creator] from [SYS_Navigation] where ParentId=@ParentId";
            SqlParameter pParentId = new SqlParameter("@ParentId", parentId);
            DataTable dataTable = DataAccess.DB.QueryDataTable(sql, new SqlParameter[] { pParentId });
            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                Navigation navigation = new Navigation();
                navigation.Id = new Guid(Convert.ToString(dataTable.Rows[index]["Id"]));
                navigation.Caption = Convert.ToString(dataTable.Rows[index]["Caption"]);
                navigation.TargetType = Convert.ToInt32(dataTable.Rows[index]["TargetType"]);
                navigation.TargetId = Convert.ToString(dataTable.Rows[index]["TargetId"]);
                navigation.CreatedTime = Convert.ToDateTime(dataTable.Rows[index]["CreatedTime"]);
                navigation.Creator = Convert.ToString(dataTable.Rows[index]["Creator"]);
                navigationList.Add(navigation);
            }

            return navigationList;
        }
    }
}
